﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DalMgr;
using DataHandle.ThreadMgr;
using PrgsMgr.CStructure;
namespace DataHandle
{
    public partial class FrmMain : Form
    {
        SftMgr sftMgr;
        public FrmMain()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            PROGRESS_INFO info = new PROGRESS_INFO();
            switch(m.Msg)
            {
                case (int)PrgsMgr.WM_MSG.WM_MSG_PROCESS_UPDATE:
                    info = (PROGRESS_INFO)m.GetLParam(info.GetType());
                    if (info.bMsg)
                    {
                        Msg(info.Status);
                    }
                    else
                    {
                        this.StatusLab.Text = info.Status;
                    }
                    
                    break;
                case (int)PrgsMgr.WM_MSG.WM_MSG_PROCESS_CLOSE_FRM:
                    this.StatusLab.Text = "就绪";
                    RunningState.Stop();
                    break;
                default:
                    break;

            }

            base.WndProc(ref m);
        }



        private void SaveCfg()
        {
            SftDataMgr.WriteCfg(PubVal.SFT_CFG, PubVal.SFT_CFG_SRC_DATA_PATH, this.tbSrcDataPath.Text);

        }
        private void LoadCfg()
        {
            string str = SftDataMgr.ReadStr(PubVal.SFT_CFG, PubVal.SFT_CFG_SRC_DATA_PATH);
            string fileName = Path.GetFileName(str);
            if (fileName != "")
            {
                string tmp = str.Replace(fileName, DateTime.Today.ToString("MM.dd")+".csv");
                if(File.Exists(tmp))
                {
                    str = tmp;
                }
            }
            this.tbSrcDataPath.Text = str;
        }

        private void tbSrcDataPath_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "csv文档(*.csv)|*.csv";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.tbSrcDataPath.Text = ofd.FileName;
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            sftMgr = new SftMgr(this.Handle);
            this.Icon = DataHandle.Properties.Resources.icon_main;
            this.Text = "数据处理 V" + Funcs.GetSftVersion();
            LoadCfg();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveCfg();
        }

        private void menuImportConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RunningState.IsRunning())
            {
                Msg("程序正在执行，请稍后再试");
            }
            else
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Excel文档(*.xlsx)|*.xlsx";
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    sftMgr.ImportData(ofd.FileName);
                }
            }
        }
        private ThdParam GetThdParam()
        {
            ThdParam param = new ThdParam();
            param.SrcConnectFilePath = this.tbSrcDataPath.Text;
            return param;
        }

        private void btnData_Click(object sender, EventArgs e)
        {

            if (RunningState.IsRunning())
            {
                Msg("程序正在执行，请稍后再试");
            }
            else
            {
                string filePath = this.tbSrcDataPath.Text;
                if (!File.Exists(filePath))
                {
                    Msg("所选文件不存在");
                    return;
                }
                sftMgr.ExportData(filePath);
            }
        }
        private delegate void dg_Msg(string str);
        private void Msg(string str)
        {
            if (this.InvokeRequired)
            {
                dg_Msg method = Msg;
                this.Invoke(method, str);
            }
            else
            {
                MessageBox.Show(str, "提醒", MessageBoxButtons.OK);
            }
        }
    }
}
